package o2o.service.order.user;

import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.order.OrderInfoMemory;
import o2o.config.dao.UserDaoSql;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.order.OrderChangeAddressRecordInfo;
import o2o.dao.PO.order.OrderInfo;
import o2o.dao.PO.user.UserAddress;
import org.springframework.stereotype.Service;

import java.sql.Connection;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: @Created by yangtk
 * @Date: @Date 2020/11/13 11:37
 * @Email: yangtongkuan@qq.com
 * @To change this template use File | Settings | File Templates.
 * @desc
 */
@Service
public class UserOrderServiceImpl implements IUserOrderService {
    @Override
    public void userOrderChangeAddress(OrderInfo orderInfo, UserAddress userAddress, String opUser) throws Exception {
        OrderChangeAddressRecordInfo recordInfo = new OrderChangeAddressRecordInfo();
        recordInfo.setCreateTime(DateUtils.getCurrTime()).setCreateUser(opUser).setOrderNo(orderInfo.getOrderNo());
        recordInfo.setDelFlag(0);
        recordInfo.setFromAddressId(orderInfo.getDeliveryId())
                .setFromProvince(orderInfo.getProvince())
                .setFromCity(orderInfo.getCity())
                .setFromCounty(orderInfo.getCounty())
                .setFromLat(orderInfo.getLat())
                .setFromLng(orderInfo.getLng())
                .setFromTelephone(orderInfo.getTelephone()).setFromBuyer(orderInfo.getBuyer())
                .setFromAddress(orderInfo.getAddress())
                .setFromAddress2(orderInfo.getAddress2());

        String address = "";
        if (ValidateUtil.isNotNull(userAddress.getAddress())) {
            address = userAddress.getAddress();
        }
        if (ValidateUtil.isNotNull(userAddress.getAddress2()) && !"null".equalsIgnoreCase(userAddress.getAddress2())) {
            address += " " + userAddress.getAddress2();
        }
        if (ValidateUtil.isNotNull(userAddress.getAddress3()) && !"null".equalsIgnoreCase(userAddress.getAddress3())) {
            address += " " + userAddress.getAddress3();
        }
        if (ValidateUtil.isNotNull(userAddress.getExtra1()) && !"null".equalsIgnoreCase(userAddress.getExtra1())) {
            address += " " + userAddress.getExtra1();
        }
        if (ValidateUtil.isNotNull(userAddress.getExtra2()) && !"null".equalsIgnoreCase(userAddress.getExtra2())) {
            address += " " + userAddress.getExtra2();
        }
        if (ValidateUtil.isNotNull(userAddress.getExtra3()) && !"null".equalsIgnoreCase(userAddress.getExtra3())) {
            address += " " + userAddress.getExtra3();
        }
//        orderInfo.setAddress(address);
        String address2 = "";
        if (ValidateUtil.isNotNull(userAddress.getAddress2()) && !"null".equalsIgnoreCase(userAddress.getAddress2())) {
            address2 = userAddress.getAddress2();
        }
        if (ValidateUtil.isNotNull(userAddress.getAddress3()) && !"null".equalsIgnoreCase(userAddress.getAddress3())) {
            address2 += " " + userAddress.getAddress3();
        }
//        orderInfo.setAddress2(address2);
//        orderInfo.setLng(userAddress.getLng());
//        orderInfo.setLat(userAddress.getLat());
//        orderInfo.setProvince(userAddress.getProvince());
//        orderInfo.setCity(userAddress.getCity());
//        orderInfo.setCounty(userAddress.getCounty());

        StringBuilder sql = new StringBuilder();
        sql.append(" update ").append(PojoTables.OrderInfo)
                .append(" set updateUser ='").append(opUser)
                .append("' ");
        if (ValidateUtil.isLatLng(userAddress.getLat(), userAddress.getLng())) {
            sql.append(" , lat =").append(userAddress.getLat())
                    .append(", lng = ").append(userAddress.getLng());
            recordInfo.setToLat(userAddress.getLat()).setToLng(userAddress.getLng());
        }
        if (ValidateUtil.isNotNull(userAddress.getCity())) {
            sql.append(" , province ='").append(ValidateUtil.isNotNull(userAddress.getProvince()) ? userAddress.getProvince() : "").append("' ");
            sql.append(" , city ='").append(ValidateUtil.isNotNull(userAddress.getCity()) ? userAddress.getCity() : "").append("' ");
            sql.append(" , county ='").append(ValidateUtil.isNotNull(userAddress.getCounty()) ? userAddress.getCounty() : "").append("' ");
            recordInfo.setToProvince(userAddress.getProvince()).setToCity(userAddress.getCity()).setToCounty(userAddress.getCounty());
        }
        sql.append(" , address ='").append(ValidateUtil.isNotNull(address) ? address : "").append("' ");
        sql.append(" , address2 ='").append(ValidateUtil.isNotNull(address2) ? address2 : "").append("' ");
        recordInfo.setToAddress(address).setToAddress2(address2);

        if (ValidateUtil.isNotNull(userAddress.getBuyer())) {
            sql.append(", buyer ='").append(userAddress.getBuyer()).append("' ");
            recordInfo.setToBuyer(userAddress.getBuyer());
        }
        if (ValidateUtil.isNotNull(userAddress.getTelephone())) {
            sql.append(", telephone ='").append(userAddress.getTelephone()).append("' ");
            recordInfo.setToTelephone(userAddress.getTelephone());
        }
        recordInfo.setToAddressId(userAddress.getId());
        sql.append(", isChangedAddress = 1 ")
                .append(" where delFlag = 0 and orderNo = '").append(orderInfo.getOrderNo()).append("' ");
        Connection conn = null;
        try {
            conn = UserDaoSql.getInstance().getConnection();
            conn.setAutoCommit(false);
            UserDaoSql.getInstance().executeBySql(sql.toString(), conn);
            UserDaoSql.getInstance().saveObj(PojoTables.ORDER_ADDRESS_CHANGE_RECORD, recordInfo, conn);
            conn.commit();
        } catch (Exception e) {
            e.getMessage();
            UserDaoSql.getInstance().rollback(conn);
            throw e;
        } finally {
            UserDaoSql.getInstance().close(conn);
        }
        // 更新缓存
        OrderInfoMemory.getInstance().modifyStatus(orderInfo.getOrderNo());
    }
}
